package com.moco.launcher.activity;

import io.netty.channel.ChannelHandlerContext;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.moco.launcher.R;
import com.moco.launcher.link.LinkManager;
import com.oazon.common.util.Utils;
import com.oazon.common.Logger;
import com.oazon.moco.lan.core.http.bean.DeleteBean;
import com.oazon.moco.lan.core.http.bean.JsonMessageType;
import com.oazon.moco.lan.core.http.bean.JsonRequest;
import com.oazon.moco.lan.core.http.bean.MediaStateInstance;
import com.oazon.moco.lan.core.http.bean.RequestClassification;
import com.oazon.moco.lan.core.http.bean.RequestOperation;
import com.oazon.moco.lan.core.http.bean.ResponseUriToQueue;
import com.oazon.moco.lan.core.http.bean.Seek;
import com.oazon.moco.lan.core.http.bean.Volume;
import com.oazon.moco.lan.core.http.bean.VolumeInstance;
import com.oazon.moco.link.core.LinkEvent;
import com.oazon.moco.link.core.LinkEventController;
import com.oazon.moco.link.core.LinkEventListener;
import com.oazon.moco.link.core.LinkSDK;
import com.oazon.moco.link.core.source.LinkEventSource;
import com.oazon.moco.link.event.SpechEvent;
import com.oazon.moco.link.event.SpeechStatus;
import com.oazon.moco.link.ui.PlayEventListener;
import com.oazon.system.bean.SystemState;
/*
 * 
vincent
 * 
 * */
public class WelcomeActivity extends BaseActivity {
	private static final String TAG = "WelcomeActivity";

	public ImageView arrow_left, arrow_right, arrow_up, arrow_down, figger;
	private final int SHOW_FIRST_SLIDE = 0x1;
	private final int SHOW_PLAY_LAYOUT =0x2;
	private final int SHOW_TIPS_LAYOUT = 0x3;
	private Timer showSlideTimer, showTipsTimer,showPlayLayoutTimer;
	Animation anim;
	AlphaAnimation animationAphaP=null;
	private LinearLayout firstSlideLayout, welIndexLayout, loveSlideLayout,
			             welShowListLayout, welShowPlayLyout,welShowTipsLayout,
			             controlPlayPause,showTwoDimensionCode,controlVolumeDown,controlVolumeUp,
			             controlSpechLayout;
	
	
    private FrameLayout welShowControlLayout;
    private Button control_iGotIt,enjoynow;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.welcome);
		
		LinkSDK linkSDK = LinkSDK.getInstance();
		linkSDK.registLinkListener(playEventListener);
		linkSDK.registLinkListener(SpechEventListener);
		arrow_left = (ImageView) findViewById(R.id.arrow_left);
		arrow_right = (ImageView) findViewById(R.id.arrow_right);
		arrow_up = (ImageView) findViewById(R.id.arrow_up);
		arrow_down = (ImageView) findViewById(R.id.arrow_down);
		figger = (ImageView) findViewById(R.id.figger);
		
		welIndexLayout = (LinearLayout) findViewById(R.id.welindex);
		firstSlideLayout = (LinearLayout) findViewById(R.id.slide);
		loveSlideLayout = (LinearLayout) findViewById(R.id.love_slide_layout);
		welShowListLayout = (LinearLayout) findViewById(R.id.wel_list);
		welShowPlayLyout = (LinearLayout) findViewById(R.id.play);
		welShowTipsLayout = (LinearLayout) findViewById(R.id.tips);
		controlPlayPause = (LinearLayout) findViewById(R.id.control_play_pause);
		controlVolumeDown = (LinearLayout) findViewById(R.id.control_v_down);
		controlVolumeUp = (LinearLayout) findViewById(R.id.control_v_up);
		controlSpechLayout = (LinearLayout) findViewById(R.id.control_voice);
		
		showTwoDimensionCode = (LinearLayout) findViewById(R.id.two_dimension_code);
		
		
		welShowControlLayout = (FrameLayout) findViewById(R.id.control_layout);
		
		
		
		control_iGotIt = (Button) findViewById(R.id.control_getit);
		control_iGotIt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				controlSpechLayout.clearAnimation();
	        	controlSpechLayout.setVisibility(View.GONE);
	        	showTwoDimensionCode.setVisibility(View.VISIBLE);
			}
		});
		
		enjoynow = (Button) findViewById(R.id.enjoynow);
		enjoynow.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				 Intent mIntent = new Intent( );   
		      	 mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
		      	 ComponentName comp = new ComponentName("com.moco.launcher", "com.moco.launcher.activity.MainActivity");    
		      	 mIntent.setComponent(comp);     
		      	 mIntent.setAction("android.intent.action.VIEW");     
		      	 startActivity(mIntent); 
			}
		});
		
		showSlideTimer = new Timer(true);
		showSlideTimer.schedule(showSlidetask, 2500);

	
	}
	private LinkEventListener SpechEventListener = new LinkEventListener(
			LinkEventController.uiSource) {

		@Override
		public void onLinkEvent(LinkEvent event) {
			
			if (filter((LinkEventSource) event.getSource())
					|| !(event instanceof SpechEvent)) {
				return;
			}
			SpechEvent e = (SpechEvent) event;
          
			if (e.getSpechStatus() == SpeechStatus.SPEECHSTART) {
				
		        if(controlSpechLayout.getVisibility()==View.VISIBLE){
		        	controlSpechLayout.clearAnimation();
		        	controlSpechLayout.setVisibility(View.GONE);
		        	showTwoDimensionCode.setVisibility(View.VISIBLE);
			        }
				
			}
		}
	};
	
	private PlayEventListener playEventListener = new PlayEventListener() {

		@Override
		protected void setVolumeDown(String uuid) {
			 if(controlVolumeDown.getVisibility()==View.VISIBLE){
			        controlVolumeDown.clearAnimation();
	   				controlVolumeDown.setVisibility(View.GONE);
					controlVolumeUp.setVisibility(View.VISIBLE);
					final AlphaAnimation animationAph = new AlphaAnimation(1, 0);
					animationAph.setDuration(1500);
					animationAph.setRepeatCount(Integer.MAX_VALUE);

					// animation.setRepeatCount(int repeatCount);//设置重复次数
					// animation.setFillAfter(boolean);//动画执行完后是否停留在执行完的状态
					// animation.setStartOffset(long startOffset);//执行前的等时间
					animationAph.setRepeatMode(Animation.REVERSE);
					controlVolumeUp.setAnimation(animationAph);

					animationAph.startNow();
					//return;
			        }
		}
		@Override
		protected void setVolumeUp(String uuid) {
			 if(controlVolumeUp.getVisibility()==View.VISIBLE){
		        	controlVolumeUp.clearAnimation();
		        	controlVolumeUp.setVisibility(View.GONE);
	   				controlSpechLayout.setVisibility(View.VISIBLE);
					final AlphaAnimation animationAph = new AlphaAnimation(1, 0);
					animationAph.setDuration(1500);
					animationAph.setRepeatCount(Integer.MAX_VALUE);

					// animation.setRepeatCount(int repeatCount);//设置重复次数
					// animation.setFillAfter(boolean);//动画执行完后是否停留在执行完的状态
					// animation.setStartOffset(long startOffset);//执行前的等待时间
					animationAph.setRepeatMode(Animation.REVERSE);
					controlSpechLayout.setAnimation(animationAph);

					animationAph.startNow();
		        }
		}



		@Override
		protected void setPlayShift(String uuid) {
			animationAphaP.cancel();
			controlPlayPause.clearAnimation();
			controlPlayPause.setVisibility(View.GONE);
			
			controlVolumeDown.setVisibility(View.VISIBLE);
			final AlphaAnimation animationApha = new AlphaAnimation(1, 0);
			animationApha.setDuration(1500);
			animationApha.setRepeatCount(Integer.MAX_VALUE);

			// animation.setRepeatCount(int repeatCount);//设置重复次数
			// animation.setFillAfter(boolean);//动画执行完后是否停留在执行完的状态
			// animation.setStartOffset(long startOffset);//执行前的等待时间
			animationApha.setRepeatMode(Animation.REVERSE);
			controlVolumeDown.setAnimation(animationApha);

			animationApha.startNow();
			
			/*
			if(controlVolumeDown.getVisibility()==View.GONE){
				controlPlayPause.setVisibility(View.GONE);
				controlVolumeDown.setVisibility(View.VISIBLE);
			}else{
				controlVolumeDown.setVisibility(View.GONE);
				controlVolumeUp.setVisibility(View.VISIBLE);
			}*/
			
			
		}

	};
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		switch (event.getAction()) {
		case MotionEvent.ACTION_UP:
			if (firstSlideLayout.getVisibility() == View.VISIBLE) {
				firstSlideLayout.setVisibility(View.GONE);
				loveSlideLayout.setVisibility(View.VISIBLE);
				final TranslateAnimation animation_up = new TranslateAnimation(
						0, 0, 0, -80);
				animation_up.setDuration(1500);// 设置动画持续时间
				animation_up.setRepeatCount(Integer.MAX_VALUE);// 设置重复次数
				animation_up.setRepeatMode(Animation.REVERSE);//
				arrow_up.setAnimation(animation_up);

				animation_up.startNow();

				final TranslateAnimation animation_down = new TranslateAnimation(
						0, 0, 0, 80);
				animation_down.setDuration(1500);// 设置动画持续时间
				animation_down.setRepeatCount(Integer.MAX_VALUE);// 设置重复次数
				animation_down.setRepeatMode(Animation.REVERSE);
				arrow_down.setAnimation(animation_down);

				animation_down.startNow();
				return true;
			}
			if (loveSlideLayout.getVisibility() == View.VISIBLE) {
				loveSlideLayout.setVisibility(View.GONE);
				welShowListLayout.setVisibility(View.VISIBLE);
				final AlphaAnimation animation_figger = new AlphaAnimation(1, 0);
				animation_figger.setDuration(1500);
				animation_figger.setRepeatCount(Integer.MAX_VALUE);

				// animation.setRepeatCount(int repeatCount);//设置重复次数
				// animation.setFillAfter(boolean);//动画执行完后是否停留在执行完的状态
				// animation.setStartOffset(long startOffset);//执行前的等待时间
				animation_figger.setRepeatMode(Animation.REVERSE);
				figger.setAnimation(animation_figger);

				animation_figger.startNow();
				return true;
			}
			break;
		}
		if (welShowListLayout.getVisibility() == View.VISIBLE) {
			welShowListLayout.setVisibility(View.GONE);
			welShowPlayLyout.setVisibility(View.VISIBLE);

			showPlayLayoutTimer = new Timer(true);
			showPlayLayoutTimer.schedule(showPlayLayoutTask, 1200);
			return true;
		}

		return super.onTouchEvent(event);
	}
	TimerTask showSlidetask = new TimerTask() {
		public void run() {
		
			Message message = new Message();

			message.what = SHOW_FIRST_SLIDE;
			wel_handler.sendMessage(message);
		}
	};
	
	TimerTask showPlayLayoutTask = new TimerTask() {
		public void run() {
			
			Message message = new Message();

			message.what = SHOW_PLAY_LAYOUT;
			wel_handler.sendMessage(message);
		}
	};
	
	TimerTask showTipsLayout = new TimerTask() {
		public void run() {
			
			Message message = new Message();

			message.what = SHOW_TIPS_LAYOUT;
			wel_handler.sendMessage(message);
		}
	};
	
	final Handler wel_handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SHOW_FIRST_SLIDE:
				
				welIndexLayout.setVisibility(View.GONE);
				firstSlideLayout.setVisibility(View.VISIBLE);
				final TranslateAnimation animation_left = new TranslateAnimation(
						0, -100, 0, 0);
				animation_left.setDuration(1500);// 设置动画持续时间
				animation_left.setRepeatCount(Integer.MAX_VALUE);// 设置重复次数
				animation_left.setRepeatMode(Animation.REVERSE);
				arrow_left.setAnimation(animation_left);
				animation_left.startNow();
				final TranslateAnimation animation_right = new TranslateAnimation(
						0, 100, 0, 0);
				animation_right.setDuration(1500);// 设置动画持续时间
				animation_right.setRepeatCount(Integer.MAX_VALUE);// 设置重复次数
				animation_right.setRepeatMode(Animation.REVERSE);
				arrow_right.setAnimation(animation_right);
				animation_right.startNow();
				showSlidetask.cancel();
				break;
			case SHOW_PLAY_LAYOUT:
				welShowPlayLyout.setVisibility(View.GONE);
				welShowTipsLayout.setVisibility(View.VISIBLE);
				showPlayLayoutTask.cancel();
				showTipsTimer = new Timer(true);
				showTipsTimer.schedule(showTipsLayout, 1500);
				break;
			case SHOW_TIPS_LAYOUT:
				welShowTipsLayout.setVisibility(View.GONE);
				welShowControlLayout.setVisibility(View.VISIBLE);
				
				 animationAphaP = new AlphaAnimation(1, 0);
				animationAphaP.setDuration(1000);
				animationAphaP.setRepeatCount(Integer.MAX_VALUE);

				// animation.setRepeatCount(int repeatCount);//设置重复次数
				// animation.setFillAfter(boolean);//动画执行完后是否停留在执行完的状态
				// animation.setStartOffset(long startOffset);//执行前的等待时间
				animationAphaP.setRepeatMode(Animation.REVERSE);
				controlPlayPause.setAnimation(animationAphaP);

				animationAphaP.startNow();
				break;
			}
			super.handleMessage(msg);
		}
	};
}
